<template>
  <el-dialog
    title="订单发货"
    :append-to-body="true"
    :visible.sync="deliveryVisible"
    width="30%"
    @open="open"
    :before-close="hidden"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="快递名称">
        <el-cascader
          placeholder="试试输入：顺。"
          :value="[form.expressType]"
          @change="classChange"
          :options="deliveryClass"
          clearable
          filterable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="物流单号">
        <el-input v-model="form.expressTradeNo"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="hidden">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  props: {
    deliveryVisible: "",
    orderID: ""
  },
  data() {
    return {
      form: {},
      deliveryClass: []
    };
  },
  methods: {
    open() {
      this.form.orderID = this.orderID;
      this.deliveryClassGetlist();
    },
    onSubmit() {
      axios.post("MallOrder/deliver", this.form).then(response => {
        if (response.data.code === 1) {
          this.$message({
            message: response.data.msg,
            type: "success",
            offset: 100
          });
          this.hidden();
        } else {
          this.$message({
            message: response.data.msg,
            type: "warning",
            offset: 100
          });
        }
      });
    },
    classChange(arr) {
      this.form.expressType = arr[arr.length - 1];
    },
    deliveryClassGetlist() {
      axios
        .post("index/getExpress", {
          page: 1,
          size: 10000
        })
        .then(response => {
          let res = response.data;
          if (res.code === 1) {
            for (let i = 0; i < res.data.list.length; i++) {
              this.deliveryClass.push({
                value: res.data.list[i].LogisticCode,
                label: res.data.list[i].ShipperCode
              });
            }
          } else {
            if (res.code === -14) {
              this.$store.commit("logout", this);
              this.$router.push({
                name: "login"
              });
            } else {
              this.$message({
                message: res.msg,
                type: "error"
              });
            }
          }
        });
    },
    hidden() {
      this.form = {};
      this.deliveryClass = [];
      this.$emit("hidden");
    }
  }
};
</script>

<style>
</style>